﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace FilesAutoPicker
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        

        private void Form1_Load(object sender, EventArgs e)
        {
            

            rtb_note.Text = "说明：\n" +
                            "   程序用于在文件众多的文件夹中选出对应的文件：\n"+
                            "   设置好源目录、输出目录后，程序将会从源文件目录中找出所有需要的文件，放到输出目录中。";

            string startupPath = Application.StartupPath;
            
            tb_outputFolder.Text = startupPath+"\\Future";
            tb_srcFolder.Text = startupPath + "\\Src";
            
            //尝试初始化FilesPicker：
            if (!FilesPicker.Init(startupPath))
            {
                MessageBox.Show("程序目录下配置文件filesSet.ini无文件内容配置。该文件配置了选取哪些文件。\n请先前往该目录，并填写其fileSet.ini的内容。\n本次程序将自动生成一个配置文件，请检查其准确性再运行本程序。");

                System.Diagnostics.Process.Start(startupPath);

                Application.Exit();
            }

            this.rtb_note.KeyDown += new KeyEventHandler(Form1_KeyDown);
        }

        void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.O)
            {
                MessageBox.Show("Application created by ZZL...\nOr dante?...\nOr 曾经的咸鱼王。");
            }
        }

        private void btn_browseSrcFolder_Click(object sender, EventArgs e)
        {
            SetFolderTo(tb_srcFolder);
        }

        private void btn_browseOutput_Click(object sender, EventArgs e)
        {
            SetFolderTo(tb_outputFolder);
            
        }

        private void SetFolderTo(TextBox textBox)
        {
            FolderBrowserDialog folderPicker = new FolderBrowserDialog();
            folderPicker.ShowNewFolderButton = true;

            DialogResult dialogResult = folderPicker.ShowDialog();


            if (dialogResult == System.Windows.Forms.DialogResult.OK)
            {
                string path = folderPicker.SelectedPath;

                textBox.Text = path;
            }
        }



        private void btnPick_Click(object sender, EventArgs e)
        {
            //获得输入路径、输出路径
            string srcFolder = tb_srcFolder.Text.Trim();
            string outputFolder = tb_outputFolder.Text.Trim();

            if (srcFolder == "" || outputFolder == "")
            {
                MessageBox.Show("Illegal param.");
                return;
            }
            else if (false == Directory.Exists(srcFolder))
            {
                MessageBox.Show("Directory not exists!");
                return;
            }
            else if (false == Directory.Exists(outputFolder))
            {
                //创建目录
                Directory.CreateDirectory(outputFolder);
            }

            bool convertToUnixFormat = cb_convertToUnix.Checked;
            bool trimEmptyLine = cb_trimEmptyLine.Checked;

            //开始处理文件
            string errorMsg;
            try
            {
                if (FilesPicker.DoPickFiles(srcFolder, outputFolder, convertToUnixFormat,trimEmptyLine, out errorMsg))
                {
                    //Procesed successfully
                    MessageBox.Show("All Done!");

                    System.Diagnostics.Process.Start(outputFolder);
                }
                else
                {
                    MessageBox.Show("Some files failed to copy:\n\n" + errorMsg);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("我擦，处理过程中粗错了！\n" + ex);
            }

            
        }

        private void cb_convertToUnix_CheckedChanged(object sender, EventArgs e)
        {
            cb_trimEmptyLine.Visible = cb_convertToUnix.Checked;

            
        }
    }
}
